<?php
/* 
* @Title:  [商品管理模型]
* @Author: {chenlei} [chenleib5@126.com]
* @Date:   2014-04-07 11:20:39
* @Last Modified by:   Administrator
* @Last Modified time: 2014-04-27 22:14:46
* @Copyright:  [hn7m.com]
*/
class GoodsModel extends Model{
	public $table="goods";
	/**
	 * [$validate 自动验证]
	 * @var array
	 */
	public $validate=array(
		);
	/**
	 * [$auto 自动添加]
	 * @var array
	 */
	public $auto=array(
		array('gadd_time','_add_time','method',2,1),
		array('qm_admin_qid','_qid','method',2,3)
		);
	protected function _add_time(){
		return time();
	}
	protected function _qid(){
		return Q('session.qid');
	}

	/**
	 * [get_data 获得所有商品数据]
	 * @param  [type] $field [字段集]
	 * @param  [type] $where [查询条件]
	 * @param  [type] $csort [排序]
	 * @return [type]        [description]
	 */
	public function get_data($field=NULL,$where=NULL,$gsort=NULL){
		return $this->field($field)->where($where)->order($gsort)->findAll();
	}
	/**
	 * [add_data 添加商品]
	 */
	public function add_data(){
		$upload=new Upload();
		$upfiles=$upload->upload();
		$bool=empty($_FILES['gindex_img']['name']);
		//*****************************添加到商品goods表*****************//
		//压首页图片到数据库goods表
		if(!$bool){
			$this->data['gindex_img']=$upfiles[0]['path'];
		}
		//压列表页图片到数据库goods表
		if(!empty($_FILES['gthumb_img']['name'])){
			$this->data['gthumb_img']=($bool)?$upfiles[0]['path']:$upfiles[1]['path'];		
		}
		//压是否上架到数据库goods表
		$gis_shelves=(Q('post.gis_shelves')=='on')?1:0;
		$this->data['gis_shelves']=$gis_shelves;
		//压商品首页推荐属性到数据库goods表
		$attr=is_array(Q('post.gattr'))?implode(',', Q('post.gattr')):NULL;
		$this->data['gattr']=$attr;
		//压商品价格和促销价格到数据库goods表
		$gprice=Q('post.gprice',0,'intval');
		$gpmt_price=Q('post.gpmt_price',0,'intval');
		$this->data['gprice']=$gprice;
		$this->data['gpmt_price']=($gpmt_price==0)?$gprice:$gpmt_price;
		$gid=$this->add();

		//**************************添加到商品详情goods_details表*****************//
		$detailsModel=K('GoodsDetails');    //加载商品详情模型
		if (!$detailsModel->create()) {		//执行商品详情模型里面的自动验证
			$this->error=$detailsModel->error;
			return false;
		}
		//压图片信息到数据库goods_details表
		if(!empty($_FILES['dbig_img']['name'])){
			if($bool){
				$dthumb=$upfiles[1]['thumb'];
				$dbig_img=$upfiles[1]['path'];
			}else{
				$dthumb=$upfiles[2]['thumb'];
				$dbig_img=$upfiles[2]['path'];
			}		
		}
		$ddescription=(isset($_POST['ddescription']))?$_POST['ddescription']:NULL;
		$data=array(
			'done_sentence'	=>Q('post.done_sentence','','htmlspecialchars'),
			'ddescription'	=>$ddescription,
			'dthumb'		=>$dthumb,
			'dbig_img'		=>$dbig_img,
			"qm_goods_gid"	=>$gid
			);
		$detailsModel->add_data($data);
		return $gid;
	}
	/**
	 * [get_onedate_value 获得某条记录值]
	 * @return [type] [description]
	 */
	public function get_onedate_value($gid,$need){
		return $this->where(array('gid'=>$gid))->getField($need);
	}
	/**
	 * [get_max 获得最大值]
	 * @return [type] [description]
	 */
	public function get_max($field){
		return $this->max($field);
	}
	/**
	 * [update_inventory 通过分配货品表来更新库存数]
	 * @return [type] [description]
	 */
	public function update_inventory($gid,$total){
		$this->where = "gid = $gid";
		$this->ginventory = "$total";
		$this->save();
	}
	/**
	 * [del_data 按主键删除商品]
	 * @param  [type] $where [description]
	 * @return [type]        [description]
	 */
	public function del_data($gid){
		if($this->where(array('gid'=>$gid))->del() && K('GoodsDetails')->del_data($gid)){
			$this->success='商品表和商品细节表删除成功!';
			return true;
		}else{
			$this->error='商品表或者商品细节表删除失败!';
			return false;
		}
	}
}
?>